package com.jlkf.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jlkf.service.util.FireBaseUtil;
import com.platform.server.response.response.pojo.ReturnData;

import net.thegreshams.firebase4j.error.FirebaseException;
import net.thegreshams.firebase4j.error.JacksonUtilityException;
import net.thegreshams.firebase4j.model.FirebaseResponse;
import net.thegreshams.firebase4j.service.Firebase;
//测试  FireBaseDataBase 数据操作
@Controller
@RequestMapping("fabFireBaseDataBase")
public class FabFireBaseDataBaseController {
	
	@RequestMapping("putData")
	@ResponseBody
	public Object putData(@RequestBody String json) {
		String firebase_baseUrl = "https://fabdriverapp.firebaseio.com/";
		// create the firebase
		Firebase firebase;
		try {
			System.out.println(firebase_baseUrl);
			firebase = new Firebase( firebase_baseUrl );
			// "DELETE" (the fb4jDemo-root)
//			FirebaseResponse response = firebase.delete(requests);

			// "PUT" (test-map into the fb4jDemo-root)
			Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
//			dataMap.put( "user1", "This was PUT into the fb4jDemo-root" );
			dataMap.put( "user1", "This was PUT into the fb4jDemo-root" );
			dataMap.put( "user2", json);
			FirebaseResponse response = firebase.put( dataMap );
			System.out.println( "\n\nResult of PUT (for the test-PUT to fb4jDemo-root):\n" + response );
			System.out.println("\n");
			return new ReturnData(200,"success");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ReturnData(500,"UnsupportedEncodingException_____fail");
		} catch (JacksonUtilityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ReturnData(500,"JacksonUtilityException——————————fail");
		} catch (FirebaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ReturnData(500,"FirebaseException——————————fail");
		}
	}
	@RequestMapping("postData")
	@ResponseBody
	public Object postData(String uId){
		String result=FireBaseUtil.postDataForFireBaseDataBase(uId, "{\"orderId\":1212,\"type\":1,\"status\":1}");
		return new ReturnData(result,200,result);
	}
	@RequestMapping("getAccess")
	@ResponseBody
	public Object getAccess(){
		String result;
		try {
			result = FireBaseUtil.getAccess();
			return new ReturnData(result,200,result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ReturnData(500,"");
		}
		
	}
}
